import { getLoadMapList, getSelectDataMapList, getTenantID, requestFunComponFiex } from '@/utils';
import { useRequest } from 'umi';
import { ILoadMapTypeAll } from '@/common';
import { adminAreadDataGet, loadMdicMap } from '@/services/BaseLayout';
import { useCallback, useMemo, useState } from 'react';
import { AssetacquisitionManageAPI } from '../common/type';
import { ManageSearchColumns } from '../mconfig';
import { disposeArea } from '../common';
import { getRequestIp } from '../../Acommon';

const MaterialManageServices = (curRes: Record<string, any>) => {
  /***获取数据字典**/
  const loadMdicIds: string[] = [`${getTenantID()}${ILoadMapTypeAll.ASSET_TYPE}`];

  const { loadMapInfo, mapLoading } = getLoadMapList([
    `${getTenantID()}${ILoadMapTypeAll.ASSET_TYPE}`, //资产类型
  ]);
  /** 资产类型 */
  const assetType = getSelectDataMapList(loadMapInfo?.[`${getTenantID()}${ILoadMapTypeAll.ASSET_TYPE}`], 'name', 'id', true);
  /***保存当前的搜索参数**/
  const [manageTableParams, setManageTableParams] = useState<Record<string, any>>({});

  /***管理页获取数据方法**/
  const { tableProps, data, loading, run } = useRequest(
    (reqParams: AssetacquisitionManageAPI.IAssetTableRequestType) => {
      let PostParams = {
        current: 1,
        pageSize: 10,
        dicTypeIds: loadMdicIds?.toString(),
        sort: 'createTime',
        order: 'desc',
        ...reqParams,
      };

      /***保存一份参数**/
      setManageTableParams(PostParams);

      /**处理一下省市县*/
      PostParams = disposeArea(PostParams);

      const reqbody: Record<string, any> = disposeArea(PostParams);

      let reqUrl;
      if (reqbody?.key === 'MaterialManageTabKey') {
        // 物料管理
        reqUrl = `${getRequestIp()}/materialController/dataGrid/${reqbody?.params}`;
      } else if (reqbody?.key === 'MyMaterialCheckTabKey') {
        // 我的审核
        reqUrl = `${getRequestIp()}/materialController/myCheck`;
      } else {
        return;
      }

      /***发起请求数据接口**/
      return requestFunComponFiex('POST', reqUrl, reqbody);
    },
    {
      manual: true,
      paginated: true,
      defaultPageSize: 10,
    },
  );

  /***重新请求的方法**/
  const reloadRequest = useCallback((reqParams: AssetacquisitionManageAPI.IAssetTableRequestType | any) => {
    /**处理创建时间*/
    if (reqParams?.createTime) {
      const [timeStart, timeEnd] = reqParams.createTime;
      reqParams['createTimeStart'] = timeStart.format('YYYY-MM-DD 00:00:00');
      reqParams['createTimeEnd'] = timeEnd.format('YYYY-MM-DD 23:59:59');
      delete reqParams['createTime'];
    }

    /**处理签发时间*/
    if (reqParams?.signTime) {
      const [timeStart, timeEnd] = reqParams.signTime;
      reqParams['signTimeStart'] = timeStart.format('YYYY-MM-DD 00:00:00');
      reqParams['signTimeEnd'] = timeEnd.format('YYYY-MM-DD 23:59:59');
      delete reqParams['signTime'];
    }

    /**处理修改时间*/
    if (reqParams?.modifyTime) {
      const [timeStart, timeEnd] = reqParams.modifyTime;
      reqParams['modifyTimeStart'] = timeStart.format('YYYY-MM-DD 00:00:00');
      reqParams['modifyTimeEnd'] = timeEnd.format('YYYY-MM-DD 23:59:59');
      delete reqParams['modifyTime'];
    }

    /**处理作废时间*/
    if (reqParams?.cancelTime) {
      const [timeStart, timeEnd] = reqParams.cancelTime;
      reqParams['cancelTimeStart'] = timeStart.format('YYYY-MM-DD 00:00:00');
      reqParams['cancelTimeEnd'] = timeEnd.format('YYYY-MM-DD 23:59:59');
      delete reqParams['cancelTime'];
    }

    delete reqParams['createFormNameDept'];
    delete reqParams['createFormNameUser'];
    if (reqParams?.createrID && reqParams?.createrID?.length > 0) {
      reqParams['createrID'] = reqParams?.createrID?.toString();
    } else {
      delete reqParams['createrID'];
    }

    delete reqParams['cancelFormNameDept'];
    delete reqParams['cancelFormNameUser'];
    if (reqParams?.cancelerID && reqParams?.cancelerID?.length > 0) {
      reqParams['cancelerID'] = reqParams?.cancelerID?.toString();
    } else {
      delete reqParams['cancelerID'];
    }

    run({ ...reqParams });
  }, []);

  /***页面数据及分页信息**/
  const tableValues = useMemo(() => {
    return {
      data,
      pagination: tableProps?.pagination,
    };
  }, [data, tableProps]);

  /***搜索功能配置columns**/
  const searchList = useMemo(() => {
    return {
      assetType,
      searchList: ManageSearchColumns(assetType),
    };
  }, [assetType, manageTableParams]);

  return {
    loading,
    reloadRequest,
    manageTableParams,
    ...tableValues, //页面列表值
    ...searchList,
  };
};

export { MaterialManageServices };
